Skip to content

Fix cyclic imports - #16

Merged
nikolaushuber merged 3 commits into
mainfrom
cyclic-imports
Nov 6, 2026
Merged

nikolaushuber merged 3 commits into
mainfrom
cyclic-imports

Conversation

@nikolaushuber

Copy link
Copy Markdown
Collaborator

Problem

Almost every module generated by the slow test suite failed to import: 203 of 204 in
generated_basic, all 204 in generated_full. CI never noticed, because the slow job only ran
py_compile.

Each theory becomes a Python module, and code equations can use constants of later theories
(e.g. Num.num_of_nat is implemented by Code_Numeral.num_of_integer, which ends up in
Arith). So the generated modules import each other; in the HOL library about 50 of them form
one import cycle. Python allows that, but module-level values such as
num_of_nat = Fun.comp(Arith.num_of_integer, ...) are evaluated during import and read
attributes of modules that are still being imported:

AttributeError: partially initialized module 'Arith' has no attribute 'num_of_integer'

A related problem: a value that calls a constant without code (e.g. X = yot_literal(...) in
Code_Test, uses_undefined = undefined) raised as soon as its module was imported.

Changes

  • Values are printed as cached functions, and their uses call them:

    @functools.cache
    def num_of_nat():
        return (Fun.comp(Arith.num_of_integer, lambda a: a))

Importing a module now evaluates nothing, so neither the cycles nor aborting constants can
break an import. Values are still evaluated only once. Aliases of functions (f_int = f)
remain ordinary functions.

Values of arity 2 or more (e.g. empty = (λ_ . None)) are printed as functions taking
all their arguments, since that is how callers call them. Before, they were curried lambdas,
and every call raised TypeError.
Applied lambdas are bracketed: (λx y. x) a b was printed as
(lambda xa: lambda : xa(a))(b), applying the body instead of the lambda.
Record name clash fixed: Name.declare drops a trailing underscore, so after declaring
point_ext
the name still counted as free, and the records point, point' and point''
of HOL-Examples.Records all became point_ext
. Names in use are now tracked separately.

Breaking change

Code that uses the generated modules must now call values: Example_nat.nat_sum() instead
of Example_nat.nat_sum.

Conflict in namify_common: main fixed the same naming bug (Name.declare
strips trailing underscores) independently; main's version is taken.
Example_tree_weight, new on main, is reseeded for values printed as
cached functions.
@nikolaushuber
nikolaushuber merged commit ca0f8ce into main Nov 6, 2026
4 checks passed
@nikolaushuber
nikolaushuber deleted the cyclic-imports branch November 6, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant